source('code/analysis/load.R')

data = read_dta('data/working/deport-sender.dta') %>%
  mutate(exp = 'deport') %>% 
  bind_rows(read_dta('data/working/defund-sender.dta') %>% mutate(exp = 'defund', replication=0)) %>% 
  bind_rows(read_dta('data/working/defund-sender-replication.dta') %>% mutate(exp = 'defund-replication', replication=1)) %>% 
  bind_rows(read_dta('data/working/rainforest-sender.dta') %>% mutate(exp = 'rainforest')) %>% 
  bind_rows(read_dta('data/working/defund-hypothetical.dta') %>% mutate(exp = 'defund-hypo')) %>%
  bind_rows(read_dta('data/working/daylight-sender.dta') %>% mutate(exp = 'dst')) %>%
  data_finalize() %>%
  mutate(exp1 = ((exp == 'defund' | exp == 'defund-replication')),
         placebo = (exp=='rainforest'|exp=='dst'),
         exp3 = exp=='deport',
         expgroup = case_when(
           exp1 ~ 'Experiments 1 and 1R',
           exp3 ~ 'Experiment 3',
           placebo ~ 'Auxiliary Experiments 2 and 3\n(placebos)'
         ),
         expgroup = factor(expgroup, levels=c('Experiments 1 and 1R','Experiment 3','Auxiliary Experiments 2 and 3\n(placebos)'))) %>%
  filter(join==1) 

main_results = function(data) {

  
  if (data$exp[1] == 'deport') {
    title = 'Willingness to post pro-deportation Tweet'
    depvar = '\\textit{Scheduled Tweet}'
    label = 'deport-sender'
    model1 = lm(posted~excuse, data=data)
    model2 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data)
    mean = formatC(mean(data$posted[data$excuse==0], na.rm = T), digits=3, format='f')
  
    out = stargazer(list(model1, model2), 
                    omit='age|race|hisp|male|rep|education|Constant|partisan',
                    covariate.labels = c('Cover'),
                    keep.stat = c('rsq','n'), dep.var.labels = depvar,
                    title = title, label = str_interp('t:${label}'),
                    add.lines = list(c('Controls', c('No', 'Yes'))))
    
    means = str_interp('No Cover mean & ${mean} & ${mean} \\\\')
  
    out = c(out[1:18], means, out[19:length(out)])
    out = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[[str_interp("${label}")]])
    
    writeLines(out, con = str_interp('output/tables/${label}.tex'))
  }
  
  if (data$exp1[1]== 1) {
    title = 'Willingness to post anti-defunding Tweet'
    depvar = '\\textit{Scheduled Tweet}'
    label = 'defund-sender'
    model1 = lm(posted~excuse+replication, data=data)
    
    model1 = lm(posted~excuse, data=data %>% filter(exp=='defund'))
    model2 = lm(posted~excuse, data=data %>% filter(exp=='defund-replication'))
    model3 = lm(posted~excuse+replication, data=data)
    model4 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='defund'))
    model5 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='defund-replication'))
    model6 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education+replication, data=data)
    
    mean =   paste(unlist(formatC(c(mean(data$posted[data$excuse==0 & data$exp=='defund'], na.rm = T),
                                    mean(data$posted[data$excuse==0 & data$exp=='defund-replication'], na.rm = T),
                                    mean(data$posted[data$excuse==0], na.rm = T),
                                    mean(data$posted[data$excuse==0 & data$exp=='defund'], na.rm = T),
                                    mean(data$posted[data$excuse==0 & data$exp=='defund-replication'], na.rm = T),
                                    mean(data$posted[data$excuse==0], na.rm = T)), digits=3)), collapse=' & ')
    
    out = stargazer(list(model1, model2, model3, model4, model5, model6), 
                    omit='age|race|hisp|male|rep|education|Constant|partisan|replication',
                    covariate.labels = c('Cover'),
                    keep.stat = c('rsq','n'), dep.var.labels = depvar,
                    column.labels = c('Main', 'Replication', 'Pooled', 'Main', 'Replication', 'Pooled'),
                    title = title, label = str_interp('t:${label}'),
                    add.lines = list(c('Controls', c(rep('No', 3)), c(rep('Yes', 3)))))
    
    means = str_interp('No Cover mean & ${mean}  \\\\')
    
    out[13] = paste0(out[13], '\\cmidrule(rr){2-4} \\cmidrule(rr){5-7}')
    out = c(out[1:8], out[11:19],means, out[20:length(out)])
    out = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["defund-sender"]])
    
    writeLines(out, con = str_interp('output/tables/${label}.tex'))
    
    
  }
  
}






## Table 1 ##
main_results(data %>% filter(exp1==1))

## Table 4 Panel A ##
main_results(data %>% filter(exp=='deport'))


## Figure 2 ##
exp1_p = get_p(data %>% filter(exp1))
exp3_p = get_p(data %>% filter(exp3))
placebo_p = get_p(data %>% filter(placebo))

means = data %>% 
  filter(exp1|exp3|placebo) %>% 
  group_by(expgroup, condition) %>% 
  summarise(mean = mean(posted, na.rm=T),
            se = sd(posted, na.rm=T)/sqrt(n()))

dodge=0.13
plot = ggplot(means, aes(x = expgroup, y =mean, shape=condition)) +
  geom_point(position = position_dodge2(width=0.5), size=3) +
  geom_linerange(aes(ymin = mean-1.96*se, ymax=mean+1.96*se), position = position_dodge2(width=0.5), alpha=0.3, size=0.8) +
  theme_bw() +
  ylab('Fraction authorizing Tweet') +
  ylim(0, 1) +
  labs(shape='Condition') +
  theme(legend.position = 'bottom',
        axis.title.x = element_blank(),
        axis.text.x = element_text(size=11)) +
  ggsignif::geom_signif(
    y_position = c(0.9, 0.9, 0.9), xmin = c(1-dodge, 2-dodge, 3-dodge), xmax = c(1+dodge, 2+dodge, 3+dodge),
    annotation = c(paste0('p',exp1_p), paste0('p',exp3_p), paste0('p=',placebo_p)), tip_length = 0.05, vjust=-0.25,
  ) 

ggsave('output/figures/pooled-sender.png', width=7, height=4, plot=plot)


